<?php

class ArticleController extends Controller
{
	public function actionIndex()
	{
	  $articles = Article::model()->getArticles();
		$this->render('index', array(
		  'articles' => $articles
		));
	}
	
	public function actionDetail($id)
	{
	  $model = $this->loadModel($id);
	  $criteria = Article::model()->search()->criteria;
    $criteria->addCondition("t.id = $model->id");
    $article  = Article::model()->find($criteria);
    
	  $related_articles = Article::model()->getArticles($model->id);
    
	  $this->render('detail', array(
	    'article' => $article,
	    'related_articles' => $related_articles
	  ));
	}
	
  public function loadModel($id)
  {
    $model = Article::model()->find(array(
      "condition" => "md5(`id`) = '$id' AND enabled = 1"
    ));
    
    if ($model === null)
      throw new CHttpException(404, 'The requested page does not exist.');
    return $model;
  }
}